package com.ruoyi.project.mobile.goods;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.goods.cart.domain.ShoppingCart;
import com.ruoyi.project.system.goods.cart.service.IShoppingCartService;
import com.ruoyi.project.system.goods.follow.domain.GoodsFollow;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.domain.GoodsDto;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goods.img.enums.GoodsImgType;
import com.ruoyi.project.system.goods.img.service.IGoodsImgService;
import com.ruoyi.project.system.goods.product.domain.GoodsProduct;
import com.ruoyi.project.system.goods.product.service.IGoodsProductService;
import com.ruoyi.project.system.history.domain.MemberGoodsHistory;
import com.ruoyi.project.system.history.service.IMemberGoodsHistoryService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.order.comment.dto.OrderCommentDto;
import com.ruoyi.project.system.order.comment.service.IOrderCommentService;
import com.ruoyi.project.system.order.commentimg.domain.CommentImg;
import com.ruoyi.project.system.order.commentimg.service.ICommentImgService;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/22 11:39
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/goods")
public class ApiGoodsController extends BaseController {

	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private IOrderCommentService commentService;
	@Autowired
	private IGoodsImgService goodsImgService;
	@Autowired
	private IGoodsProductService goodsProductService;
	@Autowired
	private ICommentImgService commentImgService;
	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private IShoppingCartService shoppingCartService;
	@Autowired
	private IMemberGoodsHistoryService memberGoodsHistoryService;
	@Autowired
	private IMemberService memberService;
	@Autowired
    private IShopConfigService shopConfigService;

	/**
	 * 查询商品详情
	 * @param id
	 * @return
	 */
	@GetMapping
	@Transient
        private R findGoodsInfo(@RequestParam String uid,@RequestParam String id){
		//查询用户信息
		Member member = this.memberService.selectMemberById(uid);
		GoodsDto goodsDto = goodsGoodsService.findGoodsDtoById(id,member.getIsVip());

        GoodsProduct goodsProduct = new GoodsProduct();
        goodsProduct.setGoodsId(id);
        List<GoodsProduct> productList = goodsProductService.selectGoodsProductList(goodsProduct);
        //分享可得最小
//        BigDecimal priceMin = new BigDecimal(0);
//        if (productList.get(0).getPriceV1Parity() != null) {
//            priceMin = productList.get(0).getPriceV1Parity();
//        }

        //分享可得最大
        BigDecimal priceMax = productList.get(0).getPriceVip();
//        if (productList.get(0).getPriceV3() != null) {
//            priceMax = productList.get(0).getPriceN().subtract(new BigDecimal(0));
//        }
        if (member.getIsVip()==1) {
            priceMax = productList.get(0).getPriceVip().subtract(productList.get(0).getPriceV1());
        }
        if (member.getIsVip()==2) {
            priceMax = productList.get(0).getPriceVip().subtract(productList.get(0).getPriceV2());
        }
        if (member.getIsVip()==3) {
            priceMax = productList.get(0).getPriceVip().subtract(productList.get(0).getPriceV3());
        }

        HashMap<String, Object> map = new HashMap<>();
		//商品信息
		map.put("goods",goodsDto);
//		map.put("goodsComment",this.commentService.findListByGoodsId(id));
		//轮播信息
		map.put("bannerImg",this.goodsImgService.findListByGoodsIdAndType(id,GoodsImgType.BANNER.getValue()));
		//详情信息
		map.put("detailsImg",this.goodsImgService.findListByGoodsIdAndType(id,GoodsImgType.DETAILS.getValue()));
		//是否关注
		map.put("isFollow",this.goodsFollowService.findByUidAndGid(uid,id)==null?false:true);
		//分享可得
        map.put("shareMoney","￥"+1+"~"+"￥"+priceMax);
        //每日爆品时间
        int sec = this.shopConfigService.findAll().get(0).getDayHotTime();//每日爆品时间 秒
        long createdTime = goodsDto.getCreated().getTime();//创建时间 时间戳
        long nowTime = DateUtils.getNowDate().getTime();//现在时间 时间戳
//        Timestamp createdTime = goodsDto.getCreated();
//        Timestamp endTime = new Timestamp(createdTime + (sec * 1000L));//结束时间 时间戳
        long endTime=createdTime+(sec * 1000L);//结束时间 时间戳
        long deadtime=endTime-nowTime;
        long dayHotTime=deadtime/1000L;
        map.put("dayHotTime",dayHotTime);
		//新增足迹信息
		MemberGoodsHistory memberGoodsHistory = new MemberGoodsHistory();
		memberGoodsHistory.setCreated(DateUtils.getNowDate());
		memberGoodsHistory.setCreateDay(DateUtils.getDate());
		memberGoodsHistory.setGoodsId(goodsDto.getId());
		memberGoodsHistory.setMemberId(uid);
		this.memberGoodsHistoryService.insertMemberGoodsHistory(memberGoodsHistory);
		return RFactory.generateR(map);
	}


	/**
	 * 查询商品评论
	 * @param uid
	 * @param id
	 * @return
	 */
	@GetMapping("/comment")
	private R findGoodsComment(@RequestParam String uid,@RequestParam String id){
		startPage();
		List<OrderCommentDto> orderCommentDtos = this.commentService.findListByGoodsId(id);
		if(orderCommentDtos!=null){
			for(OrderCommentDto orderCommentDto:orderCommentDtos){
				CommentImg commentImg = new CommentImg();
				commentImg.setCommentId(orderCommentDto.getId());
				orderCommentDto.setCommentImgs(this.commentImgService.selectCommentImgList(commentImg));
			}
		}
		return RFactory.generateR(getDataTable(orderCommentDtos));
	}

	/**
	 * 查询商品规格
	 * @param uid
	 * @param id
	 * @param spec
	 * @return
	 */
	@GetMapping("/spec")
	private R findGoodsSpec(@RequestParam String uid,@RequestParam String id,
							@RequestParam String spec){
		//查询用户信息
		Member member = this.memberService.selectMemberById(uid);
		return RFactory.generateR(this.goodsProductService.findBySpec(id,spec,member.getIsVip()));
	}


	/**
	 * 收藏 取消收藏接口
	 * @param goodsFollow
	 * @return
	 */
	@PostMapping("/follow")
	private R goodsFollow(@RequestBody GoodsFollow goodsFollow){
		//查询商品是否存在,是否已经关注 已关注则取消关注 未关注则关注
		GoodsFollow oldFollow = this.goodsFollowService.findByUidAndGid(goodsFollow.getMemberId(),goodsFollow.getGoodsId());
		if(oldFollow==null){
			goodsFollow.setCreated(DateUtils.getNowDate());
			this.goodsFollowService.insertGoodsFollow(goodsFollow);
		}else{
			this.goodsFollowService.deleteGoodsFollowById(oldFollow.getId());
		}
		return R.ok();
	}

	/**
	 * 加入购物车
	 * @param shoppingCart
	 * @return
	 */
	@PostMapping("/cart")
	public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
		//查询购物车是否存在
		ShoppingCart oldcart = this.shoppingCartService.findByGidAndPid(shoppingCart.getGoodsId(),shoppingCart.getProductId(),shoppingCart.getMemberId());

		if(oldcart==null){
			this.shoppingCartService.insertShoppingCart(shoppingCart);
		}else{
			ShoppingCart update = new ShoppingCart();
			update.setId(oldcart.getId());
			update.setTotal(oldcart.getTotal()+shoppingCart.getTotal());
			this.shoppingCartService.updateShoppingCart(update);
		}

		return R.ok();
	}

}
